<?php

namespace App\Http\Controllers\Admin;

use Validator;
use App\ShowPosition;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ShowPositionController extends Controller
{
    public function index(Request $request)
    {
        $showPositions = ShowPosition::orderBy('created_at', 'desc')->get();
        return view('admin.show_position.index', [
            'request' => $request,
            'showPositions' => $showPositions
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'bail|required|min:1|max:20|unique:show_position,name',
                'remark' => 'required'
            ];
            $messages = [
                'name.required' => '请输入位置名称！',
                'name.min' => '位置名称至少1个字符！',
                'name.max' => '位置最多20个字符！',
                'name.unique' => '此位置名称已被使用！',
                'remark.required' => '请输入备注！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'status' => 'n',
                    'info' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $showPosition = new ShowPosition();
            $showPosition->name = $data['name'];
            $showPosition->remark = $data['remark'];
            $showPosition->route = $data['route'];
            if ($showPosition->save()) {
                $return = ['status' => 'y'];
                return response()->json($return);
            } else {
                $return = ['status' => 'n', 'info' => '请稍候重试！'];
                return response()->json($return);
            }
        }
        return view('admin.show_position.add', [
            'request' => $request
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'bail|required|min:1|max:20',
                'remark' => 'required'
            ];
            $messages = [
                'name.required' => '请输入位置名称！',
                'name.min' => '位置名称至少1个字符！',
                'name.max' => '位置最多20个字符！',
                'remark.required' => '请输入备注！'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'status' => 'n',
                    'info' => $validator->errors()->first()
                ];
                return response()->json($return);
            }
            $showPosition = ShowPosition::find($data['id']);
            $showPosition->name = $data['name'];
            $showPosition->remark = $data['remark'];
            $showPosition->route = $data['route'];
            if ($showPosition->save()) {
                $return = ['status' => 'y'];
                return response()->json($return);
            } else {
                $return = ['status' => 'n', 'info' => '请稍候重试！'];
                return response()->json($return);
            }
        }
        $showPosition = ShowPosition::find($id);
        return view('admin.show_position.edit', [
            'request' => $request,
            'showPosition' => $showPosition
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $showPosition = ShowPosition::find($id);
        if ($showPosition->delete()) {
            $return = ['status' => 'y'];
        } else {
            $return = ['status' => 'n', 'info' => '请稍候重试！'];
        }
        return response()->json($return);
    }
}